/**
 * 角色管理设置
 * 
 * @version 1.0.0
 * @author yangyibao
 */
var powerTable;//权限数据表
$(function () {
	powerTable = $('#roleDataTables').DataTable({
		"bProcessing": true,
		"bLengthChange":true,
		"bServerSide": true,//服务端数据处理
		"bFilter": false,//无搜索 
		/*"iDisplayLength":8,//默认数据条数
		 */           "bSort": false,//排序功能
		 "pagingType":"full_numbers",
		 "sAjaxSource": "../power/queryRoleList",//后端服务地址
		 "fbServerData":function(sSource,aDataSet){$.ajax({"dataType":"json","type":"GET","url":sSource,"data":aDataSet});},
		 "aoColumns":[{"mDataProp":"roleId"},{"mDataProp":"roleName"},{"mDataProp":"roleDesc"},{"mDataProp":"roleCreateDate"},{"mDataProp":"roleId"}],
		 "aoColumnDefs":[{"aTargets":[0],"mRender":function(data,type,full){ if(full.isLock){return data+"<span class='label label-danger'>封</span>";}return data;}},
		                 {"aTargets":[4],"mRender":function(data,type,full){
		                	 var lockRole;
		                	 if(full.isLock) lockRole = "解封角色";
		                	 else lockRole = "封禁角色";
		                	 var operation="<div class='btn-group'><a class='btn btn-operate-left' href='#' data-toggle='dropdown'><i class='icon-user'></i> 操作</a><a href='#' type='button' class='btn btn-operate-right dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'><span class='caret'></span></a>"
		                		 operation+="<ul class='dropdown-menu'>";
		                	 operation+="<li><a href='#' id='getPermissions' data-id='"+data+"' data-value='"+full.roleName+"'> 修改功能菜单权限</a></li>";
		                	 operation+="<li><a href='#' id='uptRole' data-id='"+data+"' data-name='"+full.roleName+"' data-desc='"+full.roleDesc+"'  data-isLock='"+full.isLock+"'>编辑角色基本信息</a></li>";
		                	 operation+="<li><a href='#' id='lockRole' data-id='"+data+"' data-value='"+full.isLock+"' data-name='"+full.roleName+"'>"+lockRole+"</a></li>";
		                	 operation+="<li><a href='#' id='dltRole' data-id='"+data+"' data-name='"+full.roleName+"'>删除角色</a></li>";
		                	 operation+="</ul></div>";
		                	 return operation;
		                 }
		                 }],
		                 "oLanguage":{"sUrl": "../resources/assets/plugins/dataTables/txt/page.txt"},
		                 "initComplete":function () {
		                	 $('#roleDataTables_length').find('select').selectpicker('render');
		                	 $('#roleDataTables_length').find('select').selectpicker('refresh');
		                	 $('#roleDataTables_length').find('.btn-group').css('width','auto');
		                	 $('#roleDataTables_length').find('.btn').css('background-color','#fff');
		                	 $('#roleDataTables_length').find('.btn').css('border','#d8d8d8 solid 1px')
		                 }
	});

	//绑定查看功能模块
	$('#roleDataTables tbody').on('click','#getPermissions',function(){
		var roleId = $(this).attr("data-id");
		var setting = {
				treeId:'tree',
				check:{enable: true},
				data:{
					simpleData:{enable : true,idKey : "permissionId",pIdKey : "permissionParentId",rootPId:null},
					key:{name:"permissionName",checked: "ext1"}
				},
				callback:{
					onCheck: zTreeOnCheck
				}
		};
		$.post("../power/queryPermissionList",{roleId:roleId},function(data){
			var zTree = $.fn.zTree.init($("#treeDemo"), setting,data);
			var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			treeObj.expandAll(true);
		});

		//treeNode点击事件处理，若节点发生变化，修改按钮变为可用状态；否则为不可用状态 
		function zTreeOnCheck(event, treeId, treeNode){
			var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			var nodes = treeObj.getChangeCheckedNodes();
			for(var i=0;i<nodes.length;i++){
				nodes[i].ext2 = roleId;
			}
			if(nodes !=''){$("#uptBtn").attr("disabled",false);}
			else{$("#uptBtn").attr("disabled",true);}
		};

		$("#roleBaseInfoDiv").modal("show");
	});

	//确定按钮修改角色权限
	$("#uptBtn").click(function(){
		var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = treeObj.getChangeCheckedNodes();
		$.ajax({
			url:"../power/modifyPermissions",
			type: "POST",
			contentType:"application/json",
			dataType: "json",
			data:JSON.stringify(nodes),
			success:function(data){
				swal("成功!","角色功能菜单权限变更成功!", "success");}
		});
	});

	//删除角色操作
	$('#roleDataTables tbody').on('click','#dltRole',function(){
		var name = $(this).attr("data-name");
		var id = $(this).attr("data-id");
		confirm("您确定删除["+name+"]角色？", function(isConfirm){if (isConfirm) {$.post("../power/deleteRole",{roleId:id},function(data){if(data.flg){powerTable.ajax.reload();swal("", "当前操作成功 :)", "success");}else{swal("失败 :)", data.errorMsg, "error");}});} else {swal("取消", "您的操作已经取消 :)", "error");}});
	});

	//封禁角色操作
	$('#roleDataTables tbody').on('click','#lockRole',function(){
		var id = $(this).attr("data-id");
		var value = 1-$(this).attr("data-value");
		var name = $(this).attr("data-name");
		var lockFlg = (value)?"封禁":"解封";
		confirm("您确定要"+lockFlg+"["+name+"]角色？",function(isConfirm){if (isConfirm) {$.post("../power/lockRole",{roleId:id,isLock:value},function(data){if(data){powerTable.ajax.reload();swal("", "当前操作成功 :)", "success");}else swal("失败 :)", "当前操作失败 :)", "error");});} else  swal("取消", "您的操作已经取消 :)", "error");});
	});

	//变更角色操作
	$('#roleDataTables tbody').on('click','#uptRole',function(){
		var roleId = $(this).attr("data-id");
		var roleName = $(this).attr("data-name");
		var roleDesc = $(this).attr("data-desc");
		var roleLock = $(this).attr("data-isLock");
		$("#roleUptInfoDiv .modal-title").html('<i class="icon-user-following"></i><span id="lblAddTitle" style="font-weight:bold"> 编辑角色基本信息</span>');
		$("#roleUptInfoDiv .modal-body").html(roleUptInfoDiv(roleId,roleName,roleDesc,roleLock));
		$("#roleUptInfoDiv").modal('show');
		$("#roleUptInfoForm").validate({
			errorElement: 'span',
			errorClass:'help-inline',
			focusInvalid: false,ignore: '',
			rules: {roleName: {minlength: 2,required: true,maxlength:20,
				remote:{type:'post',url:'../power/checkRoleName',data: {roleName:function(){return $("#roleName").val();}},dataType: "json",dataFilter:function (data) {if($("#roleName").val()==roleName) return true;return  data=='false'?true:false;}}},
				roleDesc:{minlength: 2,required: true,maxlength:50}},
				messages:{roleName:{remote:'用户名已经存在请重新变更'}},
				errorPlacement: function(error,element){error.insertAfter(element);},
				highlight: function(element) {
					$(element).closest('.control-group').removeClass('success');
					$(element).closest('.help-inline').removeClass('valid');
					$(element).closest('.help-inline').removeClass('ok');
					$(element).closest('.control-group').addClass('error');
				},
				unhighlight: function(element) {$(element).closest('.control-group').removeClass('error');},
				success: function(label) {
					label.closest('.control-group').removeClass('error');
					label.closest('.control-group').addClass('success');
					label.closest('.help-inline').addClass('valid ok');
				},

				submitHandler:function(form){
					var name = $("#roleName").val();
					confirm("您确定修改["+name+"]角色？", 
							function(isConfirm){if (isConfirm) {
								$.post('../power/updateRole',
										$("#roleUptInfoForm").serialize(),
										function(data){
									if(data){$("#roleUptInfoDiv").modal("hide");
									powerTable.ajax.reload();
									swal("", "当前操作成功 :)", "success");
									}else{
										swal("取消", "您的操作已经取消 :)", "error");}
								});
							}else {swal("取消", "您的操作已经取消 :)", "error");}
					});
					return false;
				}});
	});
	$("#roleUptBtn").click(function(){
		$("#roleUptInfoForm").submit();
	});
});

function crtRoleBtn2(){
	$("#roleUptInfoDiv .modal-title").html('<i class="icon-pencil"></i><span id="lblAddTitle" style="font-weight:bold"> 角色基本信息</span>');
	$("#roleUptInfoDiv .modal-body").html(roleUptInfoDiv());
	$("#roleUptInfoDiv").modal('show');
	$("#roleUptInfoForm").validate({
		errorElement: 'span',
		errorClass:'help-inline',
		focusInvalid: false,ignore: '',
		rules: {roleName: {minlength: 2,required: true,maxlength:20,
			remote:{type:'post',url:'../power/checkRoleName',data: {roleName:function(){return $("#roleName").val();}},dataType: "json",dataFilter:function (data) {return data=='false'?true:false;}}},
			roleDesc:{minlength: 2,required: true,maxlength:50}},
			messages:{roleName:{remote:'用户名已经存在请重新变更'}},
			errorPlacement: function(error,element){error.insertAfter(element);},
			highlight: function(element) {
				$(element).closest('.control-group').removeClass('success');
				$(element).closest('.help-inline').removeClass('valid');
				$(element).closest('.help-inline').removeClass('ok');
				$(element).closest('.control-group').addClass('error');
			},
			unhighlight: function(element) {$(element).closest('.control-group').removeClass('error');},
			success: function(label) {
				label.closest('.control-group').removeClass('error');
				label.closest('.control-group').addClass('success');
				label.closest('.help-inline').addClass('valid ok');
			},
			submitHandler:function(form){
				var name = $("#roleName").val();
				confirm("您确定添加["+name+"]角色？", 
						function(isConfirm){if (isConfirm) {
							$.post('../power/saveRole',
									$("#roleUptInfoForm").serialize(),
									function(data){
								if(data){$("#roleUptInfoDiv").modal("hide");
								powerTable.ajax.reload();
								swal("", "当前操作成功 :)", "success");
								}else{
									swal("取消", "您的操作已经取消 :)", "error");}
							});
						}else {swal("取消", "您的操作已经取消 :)", "error");}
				});
				return false;
			}});
}